//
//  BBGrandTotalView.m
//  xNose
//
//  Created by ben smith on 12/09/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "BBGrandTotalView.h"
#import "BBNode.h"
#import "BBDefaultController.h"


@implementation BBGrandTotalView


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
	[self setNeedsDisplay:YES];
}


- (void)setNode:(BBNode*)aNode
{
	// we are the root view, so any children of our node need to be made into 
	// container views
	[self.node removeObserver:self forKeyPath:@"cost"];
	[self.node removeObserver:self forKeyPath:@"income"];
	
	[aNode retain];
	[node release];
	node = aNode;	
	
	[self.node addObserver:self forKeyPath:@"cost" options:NSKeyValueObservingOptionNew context:nil];
	[self.node addObserver:self forKeyPath:@"income" options:NSKeyValueObservingOptionNew context:nil];
	
	[self setNeedsDisplay:YES];
}



-(void)drawNode
{
	[self drawValueTotals];	
}

- (void)setupTextAttributes
{
	
}

-(void)drawValueTotals
{
	NSRect totalRect = NSInsetRect(self.bounds, 10, 10);
	totalRect.size.height -= 20;
	NSBezierPath * roundedRect = [NSBezierPath bezierPathWithRoundedRect:totalRect xRadius:8 yRadius:8];
	[[NSColor blackColor] set];
	[roundedRect fill];
	[[NSColor whiteColor] set];
	[roundedRect setLineWidth:2.0];
	[roundedRect stroke];
	
	if (euroImage == nil) euroImage = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"euro" ofType:@"png"]];
	
	[euroImage drawAtPoint:NSMakePoint(NSMinX(totalRect) + 5, NSMinY(totalRect) + 5) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
	
	NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
	[style setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]];
	[style setAlignment:NSCenterTextAlignment];
	
	NSDictionary * theseAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[self font],NSFontAttributeName,[NSColor whiteColor],NSForegroundColorAttributeName,style,NSParagraphStyleAttributeName,nil];
	[style release];
	
	float height = [BBDefaultController fontHeight:[self font]];
	
	// divide the rect into 5 parts, the outer parts we wont use
	NSRect fifth = NSMakeRect((NSWidth(totalRect) / 5.0) + NSMinX(totalRect), NSMidY(totalRect) - (height/2.0), NSWidth(totalRect) / 5.0, height);
	
	NSString * localString = [[NSUserDefaults standardUserDefaults] objectForKey:@"BBCostString"];	
	
	float roundTo = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBRadialRoundValue"];
	
	int costRounded = (self.node.cost / roundTo);
	costRounded *= roundTo;
	
	int incRounded = (self.node.income / roundTo);
	incRounded *= roundTo;
	
	
	NSString * cost = [NSString stringWithFormat:@"%@:  %@",[localString uppercaseString],[[BBDefaultController currencyFormatter] stringFromNumber:[NSNumber numberWithFloat:costRounded]]];
	NSAttributedString * newString = [[NSAttributedString alloc] initWithString:cost attributes:theseAttributes];
	// now draw the text on top
	[newString drawInRect:fifth];		
	[newString release];	
	
	NSBezierPath * lines = [NSBezierPath bezierPath];
	[lines moveToPoint:NSMakePoint(NSMaxX(fifth), NSMinY(fifth))];
	[lines lineToPoint:NSMakePoint(NSMaxX(fifth), NSMaxY(fifth))];
	
	fifth.origin.x += NSWidth(fifth);

	localString = [[NSUserDefaults standardUserDefaults] objectForKey:@"BBIncomeString"];	

	NSString * income = [NSString stringWithFormat:@"%@:  %@",[localString uppercaseString],[[BBDefaultController currencyFormatter] stringFromNumber:[NSNumber numberWithFloat:incRounded]]];
	newString = [[NSAttributedString alloc] initWithString:income attributes:theseAttributes];
	// now draw the text on top
	[newString drawInRect:fifth];		
	[newString release];	
	
	[lines moveToPoint:NSMakePoint(NSMaxX(fifth), NSMinY(fifth))];
	[lines lineToPoint:NSMakePoint(NSMaxX(fifth), NSMaxY(fifth))];
	
	fifth.origin.x += NSWidth(fifth);
	localString = [[NSUserDefaults standardUserDefaults] objectForKey:@"BBProfitString"];	
	NSString * profit = [NSString stringWithFormat:@"%@:  %@",[localString uppercaseString],[[BBDefaultController percentFormatter] stringFromNumber:[NSNumber numberWithFloat:self.node.profit]]];
	newString = [[NSAttributedString alloc] initWithString:profit attributes:theseAttributes];
	// now draw the text on top
	[newString drawInRect:fifth];		
	[newString release];	
	
	[lines setLineWidth:2.0];
	[lines stroke];
	
	//if (dragging) [self drawDrag];
}



@end
